<template>
  <div class="goodsBox">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row type="flex">
        <el-col :span="8">
          <div class="grid-content bg-purple title" style="text-align: left">
            <el-input
              clearable
              placeholder="请输入内容"
              v-model.trim="params.query"
              class="input-with-select"
              @keydown.native.enter="getSearchUsers"
              @clear="getSearchUsers"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getSearchUsers"
              ></el-button>
            </el-input>
          </div>
        </el-col>

        <el-col :span="8">
          <el-row type="flex">
            <el-col :span="2"></el-col>
            <el-col :span="20">
              <el-button type="primary" @click="addGoods">添加商品</el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>

      <el-table v-loading="isLoading" :data="tableData" border style="width: 100%">
        <!-- <el-table-column type="index" width="50" label="#"></el-table-column> -->
        <el-table-column width="60" prop="goods_id" label="#"></el-table-column>

        <el-table-column prop="goods_name" label="商品名称" min-width="425" class="nameItemWidth"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="130"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="220"> </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleEdit(scope.row);"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            ></el-button>    
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="params.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalSize"
    >
    </el-pagination>


    
  </div>
</template>

<script>
import {getGoods} from "../../api/index"
export default {
  data() {
    return {
      isLoading: true, //加载中显示
      tableData: [],
      totalSize: 0, //总条数
      params: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
    };
  },
  created(){
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      let res = await getGoods(this.params);
      console.log(res)
      let {data:{goods,pagenum,total},meta:{status,msg}} = res
      if(status != 200) return this.$message.error(msg)
      this.isLoading = false
      this.tableData = goods
      this.params.pagenum = +pagenum;
      this.totalSize = total;
    },
    // 搜索功能
    getSearchUsers() {
      this.getGoodsList();
    },
        // 分页功能
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.params.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.params.pagenum = val;
      this.getGoodsList();
    },
    // 修改功能
    handleEdit(){

    },
    // 删除功能
    handleDelete(){

    },
    // 添加导航
    addGoods () {
      this.$router.push({name: 'addGoods'})
    }
  },
};
</script>

<style lang="less" scoped>

</style>